package com.ruoyi.common.core.plus.web.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 * @author liyang
 * @apiNote
 * @date 2021-08-16 15:17:34
 */
public abstract class AbstractBaseCrudService<E> implements BaseCrudService<E> {


    /**
     * 获得具体实现BaseMapper
     *
     * @return
     */
    protected abstract BaseMapper<E> getMapper();

    @Override
    public E getById(Serializable id) {
        return getMapper().selectById(id);
    }

    @Override
    public E getById(Long id) {
        return getMapper().selectById(id);
    }

    @Override
    public Long count(Wrapper<E> wrapper) {
        return getMapper().selectCount(wrapper);
    }

    @Override
    public List<E> getByIds(Serializable[] ids) {
        return getMapper().selectBatchIds(Arrays.asList(ids));
    }

    @Override
    public List<E> getByList(E e, String... columns) {
        QueryWrapper<E> queryWrapper = new QueryWrapper<>(e);
        if (columns != null && columns.length > 0) {
            queryWrapper.select(columns);
        }
        return getMapper().selectList(queryWrapper);
    }

    @Override
    public List<E> getByList(Wrapper<E> wrapper) {
        return getMapper().selectList(wrapper);
    }

    @Override
    public Page<E> getByPage(Page<E> page, E e, String... columns) {
        QueryWrapper<E> queryWrapper = new QueryWrapper<>(e);
        if (columns != null && columns.length > 0) {
            queryWrapper.select(columns);
        }
        return getMapper().selectPage(page, queryWrapper);
    }

    @Override
    public Page<E> getByPage(Page<E> page, Wrapper<E> wrapper) {
        return getMapper().selectPage(page, wrapper);
    }

    @Override
    public E getByObj(Wrapper<E> wrapper) {
        return getMapper().selectOne(wrapper);
    }

    @Override
    public E getByObj(E e) {
        QueryWrapper<E> queryWrapper = new QueryWrapper<>(e);
        return getMapper().selectOne(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public E save(E e) {
        getMapper().insert(e);
        return e;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(E e) {
        return getMapper().updateById(e);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(E e, Wrapper<E> uw) {
        return getMapper().update(e, uw);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(Wrapper<E> uw) {
        return this.update(null, uw);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Serializable[] ids) {
        return getMapper().deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Wrapper<E> wrapper) {
        return getMapper().delete(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteById(Serializable id) {
        return getMapper().deleteById(id);
    }

}
